﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    public static class ExtensionArray
    {
        /// <summary>
        /// Checks if array is valid( it's not null and not empty)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <returns>returns true if is valid or throws exceptions if not valid</returns>
        /// <exception cref="NullArrayException">Array is null</exception>
        /// <exception cref="EmptyArrayException">Array is Empty</exception>
        public static bool me_IsValidThrowExceptions<T>(this T[] array)
        {
            if (array == null) throw new NullArrayException();
            if (array.Length <= 0) throw new EmptyArrayException();
            return true;
        }

        /// <summary>
        /// Checks if array is valid( it's not null and not empty)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <returns>returns true if is valid or false if not valid</returns>
        public static bool me_IsValid<T>(this T[] array)
        {
            if (array != null && array.Length > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Joins an array using a separater(can be empty);
        /// </summary>
        /// <param name="array"></param>
        /// <param name="separation">string that will be used to join all the items in the array</param>
        /// <param name="textFront">text that can be inserted before each item</param>
        /// <param name="textLast">text that can be inserted before each item</param>
        /// <returns>returns a string that represents the specified array</returns>
        public static string me_JoinArray(this string[] array, string separation, string textFront, string textLast)
        {
            string result = String.Empty;
            if (array.me_IsValid())
            {
                string tempSeparater = separation.me_GetValue();
                string tempTextFront = textFront.me_GetValue();
                string tempTextLast = textLast.me_GetValue();
                for (int i = 0; i < array.Length; i++)
                {
                    result += ((i > 0) ? tempSeparater : "") + tempTextFront + array[i].me_GetValue() + tempTextLast;
                }
            }

            return result;
        }

        public static Guid[] me_JoinArray(this string[] array)
        {
            Guid[] list = new Guid[array.Length];
            if (array.me_IsValid())
            {
                for (int i = 0; i < array.Length; i++)
                {
                    list[i] = new Guid(array[i]);
                }
            }

            return list;
        }
    }
}
